package main.gui.dataobjecteditor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import data.EditorObject;
import data.ObjectDatabase;

public class EnumEditorPanel<EnumType> extends DataAttributePanel implements
		ActionListener {

	private static final long serialVersionUID = -4916957952021916554L;

	private JComboBox enumComboBox;

	public EnumEditorPanel(String attrName, String objectGUID,
			String fqClassName, Object[] enumValues) {
		super(attrName, objectGUID, fqClassName);
		enumComboBox = new JComboBox(enumValues);
	}

	@Override
	public void buildPanel(Object value) {
		enumComboBox.setSelectedItem(value);
		enumComboBox.addActionListener(this);
		enumComboBox.setPreferredSize(new Dimension(MIN_WIDGET_WIDTH, MIN_WIDGET_HEIGHT));
		this.add(enumComboBox, BorderLayout.EAST);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void actionPerformed(ActionEvent arg0) {
		EditorObject editorObject = ObjectDatabase.getInstance()
				.getEditorObject(fqClassName, objectGUID);
		EnumType value = (EnumType) enumComboBox.getSelectedItem();
		if (editorObject.validateValue(attrName, value)) {
			editorObject.getData().put(attrName, value);
		}
	}
}
